package cxp.xuexi.maven_shiro.configuration;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.*;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Created by ASUS on 2018/2/21.
 */
@Configuration
//@ImportResource("classpath:spring/applicationContext.xml")
public class MybatisConfig implements TransactionManagementConfigurer{

    @Bean(name = "dataSource")
    public DataSource dataSource(){
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties =new Properties();
        try {
            properties.load(inputStream);
        }catch (IOException e){
            e.printStackTrace();
        }
        System.out.println((String)properties.get("jdbc.driverClassName"));
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName((String)properties.get("jdbc.driverClassName"));
        dataSource.setUrl((String)properties.get("jdbc.url"));
        dataSource.setUsername((String)properties.get("jdbc.username"));
        dataSource.setPassword((String)properties.get("jdbc.password"));
        return dataSource;
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{
        System.out.println(dataSource);

        SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("cxp.xuexi.maven_shiro.pojo");
        ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();
        sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mybatis/*Mapper.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean(name = "sqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory){
        SqlSessionTemplate sqlSessionTemplate =  new SqlSessionTemplate(sqlSessionFactory);
        System.out.println(sqlSessionTemplate);
        return sqlSessionTemplate;
    }

    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer=new MapperScannerConfigurer();
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
        mapperScannerConfigurer.setBasePackage("cxp.xuexi.maven_shiro.mapper");
        return mapperScannerConfigurer;
    }

    public PlatformTransactionManager annotationDrivenTransactionManager() {
        DataSourceTransactionManager transactionManager=new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }
}
